{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('../')\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import steward as st\n",
    "import matplotlib.pyplot as plt\n",
    "import pickle\n",
    "import xgboost\n",
    "from sklearn.metrics import roc_auc_score\n",
    "%matplotlib inline\n",
    "from src import build\n",
    "from src import test, config\n",
    "from src.feature_cols import to_drop\n",
    "import h5py\n",
    "import os\n",
    "import shutil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "build.build_all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "pj_root = config.pj_root\n",
    "train_hdf5 = pj_root + 'data/feature_train_all.hdf5'\n",
    "test_hdf5 = pj_root + 'data/feature_test_all.hdf5'\n",
    "same_mean_test_hdf5 = pj_root + 'data/feature_test_all_same_mean.hdf5'\n",
    "\n",
    "if os.path.exists(same_mean_test_hdf5) is False:\n",
    "    shutil.copy(test_hdf5, same_mean_test_hdf5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with h5py.File(train_hdf5, 'r') as f:\n",
    "    X = f['default'][:, :]\n",
    "train_mean = np.zeros(X.shape[1])\n",
    "for i in range(X.shape[1]): ##为什么X.mean(axis=0)与对每一列np.mean()不一样？？？\n",
    "    train_mean[i] = X[:, i].mean()\n",
    "del X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with h5py.File(test_hdf5, 'r') as f:\n",
    "    X = f['default'][:, :]\n",
    "test_mean = np.zeros(X.shape[1])\n",
    "for i in range(X.shape[1]): ##为什么X.mean(axis=0)与对每一列np.mean()不一样？？？\n",
    "    test_mean[i] = X[:, i].mean()\n",
    "del X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with h5py.File(same_mean_test_hdf5, 'r') as f:\n",
    "    X = f['default'][:, :]\n",
    "    for i in range(X.shape[1]):\n",
    "        col = X[:, i]\n",
    "        bool_select = np.isclose(col, test_mean[i])\n",
    "        print(bool_select.sum())\n",
    "        col[np.where(bool_select)] = train_mean[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with h5py.File(same_mean_test_hdf5, 'a') as f:\n",
    "    f['default'][:, :] = X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "np.where([True, False])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "train_all  = st.get_instance('origin/train_all').load()\n",
    "train_all.user_confirmtime.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cont = st.get_instance('basic_preprocess/cont_train_all').load()\n",
    "cont.user_confirmtime.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cont.user_confirmtime.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
